﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.ActionResults;
	using Falcon.Core.ActionResults.Enums;
	using Falcon.Core.ActionResults.ViewModels;
	using Falcon.Core.Authentication;
	using Falcon.Core.Domain;
	using Falcon.Core.Extensions;

	public class DefaultController : Controller
	{
		/// <summary>
		/// Gets the identity.
		/// </summary>
		/// <value>The identity.</value>
		public WebUserIdentity Identity
		{
			get
			{
				return Domain.User.Identity;
			}
		}

		/// <summary>
		/// Redirects to the specified route with the specified message
		/// </summary>
		/// <param name="route">The route.</param>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <returns></returns>
		protected ActionResult Message(string route, string messageOrKey)
		{
			return Message(route, null, messageOrKey, MessageScope.Success);
		}

		/// <summary>
		/// Redirects to the specified route with the specified message
		/// </summary>
		/// <param name="route">The route.</param>
		/// <param name="routeValues">The route values.</param>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <returns></returns>
		protected ActionResult Message(string route, object routeValues, string messageOrKey)
		{
			return Message(route, routeValues, messageOrKey, MessageScope.Success);
		}

		/// <summary>
		/// Redirects to the specified route with the specified message
		/// </summary>
		/// <param name="route">The route.</param>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <param name="scope">The scope.</param>
		/// <returns></returns>
		protected ActionResult Message(string route, string messageOrKey, MessageScope scope)
		{
			return Message(route, null, messageOrKey, scope);
		}

		/// <summary>
		/// Redirects to the specified route with the specified message
		/// </summary>
		/// <param name="route">The route.</param>
		/// <param name="routeValues">The route values.</param>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <param name="scope">The scope.</param>
		/// <returns></returns>
		protected ActionResult Message(string route, object routeValues, string messageOrKey, MessageScope scope)
		{
			TempData["message.result"] = new MessageViewModel
			{
				Message = Resources.Contains(messageOrKey) ? Resources.GetByKey(messageOrKey) : messageOrKey, 
				Scope = scope
			};

			return RedirectToRoute(route, routeValues);
		}

		/// <summary>
		/// Displays the specified error message.
		/// </summary>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <returns></returns>
		protected ErrorResult Error(string messageOrKey)
		{
			return Resources.Contains(messageOrKey)
				? new ErrorResult(Resources.GetByKey(messageOrKey))
				: new ErrorResult(messageOrKey);
		}

		/// <summary>
		/// Displays the specified error message.
		/// </summary>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <param name="parameters">The parameters.</param>
		/// <returns></returns>
		protected ErrorResult Error(string messageOrKey, params object[] parameters)
		{
			return Resources.Contains(messageOrKey)
				? new ErrorResult(Resources.GetByKey(messageOrKey, parameters))
				: new ErrorResult(messageOrKey.Fmt(parameters));
		}

		/// <summary>
		/// Displays the specified status message.
		/// </summary>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <returns></returns>
		protected StatusResult Status(string messageOrKey)
		{
			return Resources.Contains(messageOrKey)
				? new StatusResult(Resources.GetByKey(messageOrKey))
				: new StatusResult(messageOrKey);
		}

		/// <summary>
		/// Displays the specified status message.
		/// </summary>
		/// <param name="messageOrKey">The message or resource key.</param>
		/// <param name="parameters">The parameters.</param>
		/// <returns></returns>
		protected StatusResult Status(string messageOrKey, params object[] parameters)
		{
			return Resources.Contains(messageOrKey)
				? new StatusResult(Resources.GetByKey(messageOrKey, parameters))
				: new StatusResult(messageOrKey.Fmt(parameters));
		}

		/// <summary>
		/// Displays the not authorized message.
		/// </summary>
		/// <returns></returns>
		protected ErrorResult NotAuthorized()
		{
			return Error(Resources.Messages.NotAuthorized);
		}

		/// <summary>
		/// Displays the specified text.
		/// </summary>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		protected TextResult Text(string value)
		{
			return new TextResult { Text = value };
		}
	}
}